//
//  MBSpceGradeViewController.m
//  MiBaoTu
//
//  Created by admin on 16/9/21.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBSpceGradeViewController.h"

#import "MBSpaceGradeHeaderView.h"
#import "MBSpaceGradeRuleCell.h"
#import "MBSpaceGradeDescribeCell.h"

@interface MBSpceGradeViewController ()<UITableViewDataSource,UITableViewDelegate> {
    UITableView                 *_tableView;
    MBSpaceGradeHeaderView      *_headerView;
    NSMutableArray              *_ruleArr;
    NSMutableArray              *_scribeArr;
}

@end

@implementation MBSpceGradeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _ruleArr = [[NSMutableArray alloc]initWithObjects:@[@"等级",@"昵称",@"所需积分"],@[@"Lv.1",@"婴幼儿",@"0-100"],@[@"Lv.2",@"幼儿园",@"100-300"],@[@"Lv.3",@"小学生",@"300-800"],@[@"Lv.4",@"初中生",@"800-1500"],@[@"Lv.5",@"高中生",@"1500-3000"],@[@"Lv.6",@"大学生",@"3000-6000"],@[@"Lv.7",@"研究生",@"6000-10000"],@[@"Lv.8",@"博士生",@"10000-20000"], nil];
    _scribeArr = [[NSMutableArray alloc]initWithObjects:@"签到+1分（登录后自动签到）",@"播放儿歌、故事+1分（每日3分封顶） ",@"分享内容+1分（每日3分封顶）",@"下载资源+1分（每日3分封顶）",@"发布主题+1分（每日6分封顶）",@"发布亲子秀+3分（每日9分封顶）",@"主题加精+10分（不限制）", nil];
    // Do any additional setup after loading the view.
    [self setTitleView:@"我的等级"];
    [self setBackItem];
    
    [self onCreateUI];
    
    [self onLoadData];
}

#pragma mark - Actions
#pragma mark - UI
- (void)onCreateUI
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.backgroundView = nil;
    [self.view addSubview:_tableView];
    _tableView.tableHeaderView = [self setTableHeaderView];
}

- (void)onUpdateUIForView:(UIDeviceOrientation)orientation
{
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.view.height);
     _tableView.tableHeaderView = [self setTableHeaderView];
    [_tableView reloadData];
}

- (MBSpaceGradeHeaderView *)setTableHeaderView
{
    if (!_headerView) {
        _headerView = [[MBSpaceGradeHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160*PERCEN)];
    }
    _headerView.orientation = self.orientation;
    return _headerView;
}

#pragma mark - 数据加载
- (void)onLoadData
{
    UserModel *model = [UserManager standerDefult].userModel;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [[HTTPManager standerDefault]AFGetRequest:[NSString stringWithFormat:SPACE_GETGRADE_URL_PATH,model.UserID] view:nil success:^(id json) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        if ([json isKindOfClass:[NSDictionary class]]) {
            if ([json[@"ret_code"] intValue] == 0) {
                NSDictionary *dic = [json objectForKey:@"data"];
                _headerView.dataDic = dic;
                [_tableView reloadData];
            } 
        }
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [_tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return _scribeArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        //规则
        static NSString *cellID = @"Grade_Rule_Cell";
        MBSpaceGradeRuleCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[MBSpaceGradeRuleCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.orientation = self.orientation;
        cell.ruleArr = _ruleArr;
        return cell;
    }
    else if (indexPath.section == 1) {
        //描述
        static NSString *cellID = @"Grade_Describe_Cell";
        MBSpaceGradeDescribeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[MBSpaceGradeDescribeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.orientation = self.orientation;
        if (indexPath.row < _scribeArr.count) {
            cell.scriptStr = [_scribeArr objectAtIndex:indexPath.row];
        }
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (ISIPAD) {
            return 780*PERCEN;
        }
        return 580*PERCEN;
    }
    else if (indexPath.section == 1) {
        return 80*PERCEN;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    bgView.backgroundColor = UIColorFromHex(0xf5f5f5);

    UILabel *_gradeLb = [[UILabel alloc]initWithFrame:CGRectMake((ISIPAD?460*PERCEN*(self.orientation==UIDeviceOrientationPortrait?1.0*SCREEN_WIDTH/SCREEN_HEIGHT:1.):25*PERCEN), 5, 200, 20)];
    _gradeLb.backgroundColor = [UIColor clearColor];
    _gradeLb.textAlignment = NSTextAlignmentLeft;
    _gradeLb.textColor = UIColorFromHex(0x999999);
    _gradeLb.font = [UIFont systemFontOfSize:13.f];
    if (section == 0) {
        _gradeLb.text = [NSString stringWithFormat:@"升级规则简介(共8级)"];
    } else {
        _gradeLb.text = [NSString stringWithFormat:@"积分获取规则"];
    }
    [bgView addSubview:_gradeLb];
    
    return bgView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}

@end
